﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using KvoMvc.BusinessEntity;
using KvoMvc.BusinessContract;
using System.Web.Security;

namespace KvoMvc.ServiceProxy
{
    public interface ISecurityManagerProxyChannel : ISecurityManager, IClientChannel
    { } 

    public class SecurityManagerProxy
    {
        /// <summary>
        /// Validate user.
        /// </summary>
        /// <param name="logOn"></param>
        /// <returns></returns>
        public static bool IsValidUser(LogOn logOn)
        {
            ISecurityManagerProxyChannel manager = ProxyHelper.CreateRemoteObject<ISecurityManagerProxyChannel>("KvoMvc.BusinessContract.ISecurityManager");
            using (OperationContextScope scope = new OperationContextScope(manager))
            {

                bool result = manager.IsValidUser(logOn);
                ((IClientChannel)manager).Close();
                return result;
            }
        }

        /// <summary>
        /// Set authenticate cookie.
        /// </summary>
        /// <param name="logOn"></param>
        public static void SetAuthCookie(string userName, bool isCreatePersistentCookie)
        {
            ISecurityManagerProxyChannel manager = ProxyHelper.CreateRemoteObject<ISecurityManagerProxyChannel>("KvoMvc.BusinessContract.ISecurityManager");
            using (OperationContextScope scope = new OperationContextScope(manager))
            {

                manager.SetAuthCookie(userName, isCreatePersistentCookie);
                ((IClientChannel)manager).Close(); 
            }
        }

        /// <summary>
        /// Sign out.
        /// </summary>
        public static void SignOut()
        {
            ISecurityManagerProxyChannel manager = ProxyHelper.CreateRemoteObject<ISecurityManagerProxyChannel>("KvoMvc.BusinessContract.ISecurityManager");
            using (OperationContextScope scope = new OperationContextScope(manager))
            {

                manager.SignOut();
                ((IClientChannel)manager).Close();
            }
        }

        /// <summary>
        /// Create new user.
        /// </summary>
        /// <param name="register"></param>
        /// <returns></returns>
        public static MembershipCreateStatus CreateUser(Register register)
        {
            ISecurityManagerProxyChannel manager = ProxyHelper.CreateRemoteObject<ISecurityManagerProxyChannel>("KvoMvc.BusinessContract.ISecurityManager");
            using (OperationContextScope scope = new OperationContextScope(manager))
            {

                MembershipCreateStatus result = manager.CreateUser(register);
                ((IClientChannel)manager).Close();
                return result;
            }
        }
    }
}
